#!/bin/bash

# AI-CRM 项目启动脚本

echo "================================"
echo "  AI-CRM 部署启动脚本"
echo "================================"
echo ""

# 检查 Docker 是否安装
if ! command -v docker &> /dev/null; then
    echo "错误: 未检测到 Docker，请先安装 Docker"
    exit 1
fi

# 检查 Docker Compose 是否安装
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
    echo "错误: 未检测到 Docker Compose，请先安装 Docker Compose"
    exit 1
fi

# 创建必要的目录
echo "正在创建数据目录..."
mkdir -p data/shared-mysql
mkdir -p data/cordys-redis
mkdir -p data/cordys-crm/files
mkdir -p data/sqlbot-postgres
mkdir -p data/sqlbot
mkdir -p data/dataease
mkdir -p data/one-api
mkdir -p logs/cordys-crm
mkdir -p logs/dataease
mkdir -p logs/nginx
mkdir -p config/shared-mysql
mkdir -p config/nginx/conf.d
mkdir -p config/nginx/html
mkdir -p config/nginx/ssl
mkdir -p config/cordys-redis
mkdir -p config/cordys-crm
mkdir -p config/dataease

echo "目录创建完成"
echo ""

# 启动服务
echo "正在启动服务..."
if docker compose version &> /dev/null; then
    docker compose up -d
else
    docker-compose up -d
fi

if [ $? -eq 0 ]; then
    echo ""
    echo "================================"
    echo "  服务启动成功！"
    echo "================================"
    echo ""
    echo "🌐 统一访问入口（推荐）："
    echo "----------------------------"
    echo "导航页面: http://localhost"
    echo ""
    echo "通过 Nginx 访问："
    echo "  Cordys CRM:  http://localhost/crm"
    echo "  SQLBot:      http://localhost/sqlbot"
    echo "  DataEase:    http://localhost/dataease"
    echo "  One API:     http://localhost/one-api"
    echo ""
    echo "📱 直接访问（原始端口）："
    echo "----------------------------"
    echo "1. Cordys CRM"
    echo "   前端: http://localhost:8081"
    echo "   后端: http://localhost:8082"
    echo "   默认账号: admin"
    echo "   默认密码: CordysCRM"
    echo ""
    echo "2. SQLBot"
    echo "   访问: http://localhost:8000"
    echo "   默认账号: admin"
    echo "   默认密码: SQLBot@123456"
    echo ""
    echo "3. DataEase"
    echo "   访问: http://localhost:8083"
    echo "   默认账号: admin"
    echo "   默认密码: dataease"
    echo ""
    echo "4. One API"
    echo "   访问: http://localhost:8084"
    echo "   默认账号: root"
    echo "   默认密码: 123456"
    echo ""
    echo "🗄️ 数据库端口："
    echo "----------------------------"
    echo "共享 MySQL (Cordys + DataEase): localhost:3306"
    echo "SQLBot PostgreSQL: localhost:5432"
    echo "Cordys Redis: localhost:6379"
    echo ""
    echo "查看日志命令："
    if docker compose version &> /dev/null; then
        echo "docker compose logs -f [服务名]"
    else
        echo "docker-compose logs -f [服务名]"
    fi
    echo ""
    echo "停止服务命令："
    if docker compose version &> /dev/null; then
        echo "docker compose down"
    else
        echo "docker-compose down"
    fi
    echo "================================"
else
    echo ""
    echo "错误: 服务启动失败，请检查日志"
    exit 1
fi

